import 'package:inmotion_pro/l10n/local.dart';
import 'package:inmotion_pro/utils/date_format_util.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:safemap/safemap.dart';

/// 申请信息状态；1 - 待处理；2 - 已同意；3 - 已拒绝；4 - 已失效
enum AuthApplicationState {
  pending(1),
  approved(2),
  rejected(3),
  expired(4);

  final int value;
  const AuthApplicationState(this.value);

  String get name {
    switch (this) {
      case AuthApplicationState.pending:
        return local.vehicle_authorization_pending;
      case AuthApplicationState.approved:
        return local.vehicle_authorization_approved;
      case AuthApplicationState.rejected:
        return local.vehicle_authorization_rejected;
      case AuthApplicationState.expired:
        return local.vehicle_authorization_expired;
      default:
        return '';
    }
  }
}

/// 授权申请记录模型
class AuthApplicationModel extends BaseData {
  int? id;

  String? sn;

  /// 产品型号，hex代码
  String? modelCode;

  /// 产品型号名称
  String? modelName;

  // 蓝牙名称
  String? bleName;

  /// 申请用户信息
  BindApplyUser? fromUser;

  BindApplyUser? toUser;

  /// 创建时间（时间戳）
  int? createdAt;

  /// 更新时间（时间戳）
  int? updatedAt;

  /// 申请信息状态；1 - 待处理；2 - 已同意；3 - 已拒绝；4 - 已失效
  AuthApplicationState? state;

  /// 申请时间（具体时间）
  get applyTime {
    return DateFormatUtil.formatTimestamp(createdAt ?? 0);
  }

  /// 处理时间（具体时间）
  get updateTime {
    return DateFormatUtil.formatTimestamp(updatedAt ?? 0);
  }

  AuthApplicationModel({
    this.id,
    this.sn,
    this.modelCode,
    this.modelName,
    this.bleName,
    this.fromUser,
    this.toUser,
    this.createdAt,
    this.updatedAt,
    this.state,
  });

  factory AuthApplicationModel.fromJson(Map<dynamic, dynamic> map) {
    SafeMap safeMap = SafeMap(map);
    return AuthApplicationModel(
      id: safeMap['id'].toInt,
      sn: safeMap['sn'].string,
      modelCode: safeMap['model_code'].string,
      modelName: safeMap['model_name'].string,
      bleName: safeMap['device_name'].string,
      fromUser: BindApplyUser.fromMap(safeMap['from_user'].map),
      toUser: BindApplyUser.fromMap(safeMap['to_user'].map),
      createdAt: safeMap['created_at'].toInt,
      updatedAt: safeMap['updated_at'].toInt,
      state: AuthApplicationState.values.firstWhere(
        (e) => e.value == safeMap['state'].toInt,
        orElse: () => AuthApplicationState.pending,
      ),
    );
  }
}

/// 绑定申请用户
class BindApplyUser {
  int? id;
  String? nickName;
  String? avatar;

  BindApplyUser({
    this.id,
    this.nickName,
    this.avatar,
  });

  BindApplyUser.fromMap(Map<dynamic, dynamic>? map) {
    if (map == null) {
      return;
    }
    id = map['id'];
    nickName = map['nickname'];
    avatar = map['avatar'];
  }
}
